home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 1 / PC World Interactive 1 - Nisan 1997.iso / nostalji / bbs / music / sbbook.arj / SBBOOK / SOURCE / SBSIMDMO / FILEFUNC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-22  |  4.3 KB  |  175 lines

  1. #include <dos.h>
  2. #include <string.h>
  3. #include <alloc.h>
  4. #include "drvrfunc.h"
  5. #include "filefunc.h"
  6.  
  7. char *dirMemErr = "Unable to allocate enough memory for directory";
  8.  
  9. /*************************************************************************
  10.  * FUNCTION: INITFILELIST - Initializes linked list that filenames are
  11.  *                          to be stored in.
  12.  *
  13.  * Inputs: *ptr - Head pointer to linked list.
  14.  *         lines - Number of lines that will be displayed in the windows.
  15.  *
  16.  * Output: none
  17.  *************************************************************************/
  18. void InitFileList(FILEPTR *ptr, int lines)
  19. {
  20.     int i;
  21.  
  22.     ptr->active = NULL;
  23.  
  24.     ptr->start = ptr->stop = ptr->current = ptr->head;
  25.     for(i = 0; (i < (lines - 1)) && (ptr->stop->next != NULL); i++)
  26.         ptr->stop = ptr->stop->next;
  27. }
  28.  
  29. /*************************************************************************
  30.  * FUNCTION: GETFILELIST - Builds linked list of sorted filenames.
  31.  *
  32.  * Inputs: **files - Head pointer to linked list.
  33.  *         *pathName - Path and extension of files to add to linked list.
  34.  *
  35.  * Output: OK if no error, MEMERR if process runs out of memory.
  36.  *************************************************************************/
  37. int GetFileList(FILELEMENT **files, char *pathName)
  38. {
  39.     struct find_t fileInfo;
  40.     FILELEMENT *entry;
  41.  
  42.     if(_dos_findfirst(pathName, _A_NORMAL, &fileInfo) == OK)
  43.         if((entry = (FILELEMENT *)malloc(sizeof(FILELEMENT))) != NULL)
  44.         {
  45.             strcpy(entry->filename, fileInfo.name);
  46.             PadBlanks(entry->filename, 12);
  47.             entry->status = STOPPED;
  48.             Add(files, entry);
  49.         }
  50.         else
  51.             return(MEMERR);
  52.     else
  53.         return(FILERR);
  54.  
  55.     while(_dos_findnext(&fileInfo) == OK)
  56.     {
  57.         if((entry = (FILELEMENT *)malloc(sizeof(FILELEMENT))) != NULL)
  58.         {
  59.             strcpy(entry->filename, fileInfo.name);
  60.             PadBlanks(entry->filename, 12);
  61.             entry->status = STOPPED;
  62.             Add(files, entry);
  63.         }
  64.         else
  65.             return(MEMERR);
  66.     }
  67.     return(OK);
  68. }
  69.  
  70. /*************************************************************************
  71.  * FUNCTION: ADD - Adds an element to a linked list.
  72.  *
  73.  * Inputs: **fileList - Pointer to linked list.
  74.  *         *newEntry -  Element to add.
  75.  *
  76.  * Output: none
  77.  *************************************************************************/
  78. void Add(FILELEMENT **fileList, FILELEMENT *newEntry)
  79. {
  80.     int found = NO;
  81.     int test;
  82.     char tempStr[13];
  83.     FILELEMENT *files = *fileList;
  84.  
  85.     if(files != NULL)
  86.     {
  87.         while(found == NO)
  88.         {
  89.             test = strcmpi(files->filename, newEntry->filename);
  90.             if(test == 0)
  91.                 found = YES;
  92.             else
  93.                 if(test > 0)
  94.                 {
  95.                     newEntry->prev = files->prev;
  96.                     if(files->prev == NULL)
  97.                     {
  98.                         newEntry->next = *fileList;
  99.                         files->prev = newEntry;
  100.                         *fileList = newEntry;
  101.                     }
  102.                     else
  103.                     {
  104.                         newEntry->next = files;
  105.                         files->prev->next = newEntry;
  106.                         files->prev = newEntry;
  107.                     }
  108.                     found = YES;
  109.                 }
  110.                 else
  111.                 {
  112.                     if(files->next != NULL)
  113.                         files = files->next;
  114.                     else
  115.                     {
  116.                         newEntry->prev = files;
  117.                         newEntry->next = NULL;
  118.                         files->next = newEntry;
  119.                         found = YES;
  120.                     }
  121.                 }
  122.         }
  123.     }
  124.     else
  125.     {
  126.         *fileList = newEntry;
  127.         newEntry->prev = NULL;
  128.         newEntry->next = NULL;
  129.     }
  130. }
  131.  
  132. /*************************************************************************
  133.  * FUNCTION: PADBLANKS - Right pad string with blanks.
  134.  *
  135.  * Inputs: string - String to pad.
  136.  *         size - Total number of characters in string (including blanks).
  137.  *
  138.  * Output: none
  139.  *************************************************************************/
  140. void PadBlanks(char string[], int size)
  141. {
  142.     int i;
  143.  
  144.     for(i = strlen(string); i < size; i++)
  145.         string[i] = ' ';
  146.     string[size] = 0;
  147. }
  148.  
  149. /*************************************************************************
  150.  * FUNCTION: FINDFILETYPE - Returns file type of a given filename.
  151.  *
  152.  * Inputs: filename - Filename to analyze.
  153.  *
  154.  * Output: File type.
  155.  *************************************************************************/
  156. int FindFileType(char filename[])
  157. {
  158.     int i = 0;
  159.     int length = strlen(filename);
  160.  
  161.     for(i = 0; (i < length) && (filename[i] != '.'); i++);
  162.     if(i != length)
  163.         switch(filename[i + 1])
  164.         {
  165.             case 'M':
  166.                 return(Midi);
  167.             case 'C':
  168.                 return(FM);
  169.             case 'V':
  170.                 return(DskVoice);
  171.         }
  172.     return(FILERR);
  173. }
  174.  
  175.